<template>
  <div class="list-page">
    <div class="list-page-search" v-if="tableInfo.showSearch">
      <NForm
        inline
        :model="tableInfo.queryForm"
        label-width="auto"
        label-placement="left"
      >
        <NFormItem label="标签">
          <NInput
            v-model:value="tableInfo.queryForm.name"
            placeholder="请输入标签"
            clearable
            style="width: 200px"
          />
        </NFormItem>
        <NFormItem>
          <NButton type="primary" @click="queryData">
            <template #icon>
              <n-icon>
                <i class="esayIcon esayIcon-sousuo"></i>
              </n-icon>
            </template>
            <span>搜索</span>
          </NButton>
        </NFormItem>
      </NForm>
    </div>
    <div class="list-page-main">
      <div class="pb20 flex flex-between">
        <div>
          <NButton type="primary" @click="handAdd()">
            <i class="esayIcon esayIcon-tianjiawenjianjia"></i>
            <span class="pl8">添加</span>
          </NButton>
          <NButton class="ml10" type="error" @click="handDel()">
            <i class="esayIcon esayIcon-shanchu"></i>
            <span class="pl8">批量删除</span>
          </NButton>
        </div>
        <div>
          <n-button
            tertiary
            circle
            @click="tableInfo.showSearch = !tableInfo.showSearch"
          >
            <template #icon>
              <i class="esayIcon esayIcon-sousuo"></i>
            </template>
          </n-button>
          <n-button class="ml15" tertiary circle @click="handToast">
            <template #icon>
              <i class="esayIcon esayIcon-daochu"></i>
            </template>
          </n-button>
          <n-button class="ml15" tertiary circle @click="handToast">
            <template #icon>
              <i class="esayIcon esayIcon-daoru"></i>
            </template>
          </n-button>
        </div>
      </div>

      <NDataTable
        style="flex: 1"
        flex-height
        :columns="columns"
        :row-key="rowKey"
        :loading="tableInfo.listLoading"
        :data="tableInfo.tableData"
      />
      <div class="pagination mt20">
        <NPagination
          v-model:page="tableInfo.queryForm.pageNo"
          :page-count="tableInfo.total"
          :page-sizes="tableInfo.pageSizes"
          show-quick-jumper
          show-size-picker
          @updatePage="pageChange"
          @updatePageSize="sizeChange"
        />
      </div>
    </div>
    <Edit ref="addRef" @success="queryData" />
    <DelConfirm ref="delRef" @confirm="delConfirm" />
  </div>
</template>

<script setup lang="ts">
import { h, ref } from 'vue'
import {
  NButton,
  NDataTable,
  NForm,
  NFormItem,
  NIcon,
  NInput,
  NPagination,
  useMessage
} from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import tagApi from '@/service/api/photo/tag'
import Edit from './components/Edit.vue'
import { useApi } from '@/hooks/useApi'
import DelConfirm from '@/components/nComponents/DelConfirm'
const rowKey = (row: any) => row.id
const addRef = ref()
const delRef = ref()

const {
  handAdd,
  pageChange,
  sizeChange,
  tableInfo,
  queryData,
  handDel,
  delConfirm
} = useApi({
  addRef,
  delRef,
  getData: async (params: any) => {
    const res = await tagApi.list(params)
    res.data.data.map((el: any) => {
      el.key = el.id
      return el
    })
    return res
  },
  delData: tagApi.dels
})
/**
 *  获取列表
 * */
const columns: DataTableColumns<any> = [
  {
    type: 'selection',
    options: ['all', 'none'],
    disabled(row) {
      return row.name === 'Edward King 3'
    }
  },
  {
    title: '标签名称',
    key: 'name'
  },
  {
    title: '权重',
    key: 'sort'
  },
  {
    title: '操作',
    key: 'total',
    align: 'center',
    width: 300,
    render(row: any) {
      return h('div', [
        h(
          NButton,
          {
            size: 'small',
            type: 'warning',
            class: 'ml10',
            onClick: () => handAdd(row)
          },
          { default: () => '编辑' }
        ),
        h(
          NButton,
          {
            size: 'small',
            class: 'ml10',
            type: 'error',
            onClick: () => handDel(row)
          },
          { default: () => '删除' }
        )
      ])
    }
  }
]

const toast = useMessage()
const handToast = () => {
  toast.warning('开发中')
}
</script>

<style lang="scss" scoped></style>
